package com.qianfeng.wuyang.himalaya.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.adapter.broadcastadapter.ProvinceFragmentPagerAdapter;
import com.qianfeng.wuyang.himalaya.bean.broadcastbean.ProvinceListEntity;
import com.qianfeng.wuyang.himalaya.bean.broadcastbean.ProvinceListResultEntity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ProvinceActivity extends AppCompatActivity implements View.OnClickListener, Callback<ProvinceListEntity> {
//    private RecyclerView recycler_ProvinceActivity;
    private ImageView back;
    private List<ProvinceListResultEntity> list;//tablayout的列表集合
    private ViewPager viewpager_ProvinceActivity;
    private TabLayout tablayout_ProvinceActivity;
    private ProvinceFragmentPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_province);
        initView();
        new Retrofit.Builder()
                .baseUrl("http://live.ximalaya.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(BCProvinceService.class)
                .getList().enqueue(this);
        adapter = new ProvinceFragmentPagerAdapter(getSupportFragmentManager(),list);
        viewpager_ProvinceActivity.setAdapter(adapter);
        tablayout_ProvinceActivity.setupWithViewPager(viewpager_ProvinceActivity);
    }
    private void initView() {
//        recycler_ProvinceActivity = ((RecyclerView) findViewById(R.id.recycler_ProvinceActivity));
        back = ((ImageView) findViewById(R.id.image_back));
        tablayout_ProvinceActivity = ((TabLayout) findViewById(R.id.tablayout_ProvinceActivity));
        back.setOnClickListener(this);
        list = new ArrayList<>();
        viewpager_ProvinceActivity = ((ViewPager) findViewById(R.id.viewpager_ProvinceActivity));
    }

    @Override
    public void onClick(View view) {
        finish();
    }

    @Override
    public void onResponse(Call<ProvinceListEntity> call, Response<ProvinceListEntity> response) {
        Log.i("bodyprovince",""+response.body().getMsg());
        list.addAll(response.body().getResult());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<ProvinceListEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, t.getMessage(), Toast.LENGTH_SHORT).show();

    }
}
